<template>
	<view class="order-index">
		<u-sticky>
			<u-subsection :list="billStypes" :current="navbarCurrent" @change="billStypesChange" activeColor="#FF5722" :fontSize="28" mode="button"></u-subsection>
		</u-sticky>
		<view class="order-list">
			<u-empty
			    mode="list"
				:iconSize="100"
			    icon="order"
				:show="!records.length"
				:marginTop="100"
				:textSize="30"
			>
			</u-empty>
			
			<view class="order-item" v-for="(item,key) in records" :key="key">
				<view class="order-item__info flex">
					<view class="title">发生类型</view>
					<view class="value">{{ item.stype_display }}</view>
				</view>
				
				<view class="order-item__info flex">
					<view class="title">发生数量</view>
					<view class="value flex">
						<u-text :text="item.amt" :size="32" :bold="true" :color="item.amt>0?'#FF0000':'#4cd964'" ></u-text>
					</view>
				</view>
				<view class="order-item__info flex">
					<view class="title">当期余额</view>
					<view class="value">{{ item.balance }}</view>
				</view>
				<view class="order-item__info flex">
					<view class="title">账单说明</view>
					<view class="value">{{ item.remark }}</view>
				</view>
				<view class="order-item__info flex">
					<view class="title">兑换时间</view>
					<view class="value">
						<u-text :size="32" :text="item.create_time"></u-text>
					</view>
				</view>
			</view>
		</view>
		
		<u-loadmore :status="loadStatus" fontSize="32" :iconSize="32" line />
	</view>
</template>

<script>
	import { getUserGoldsBills } from '@/api/api.js'
	export default {
		data() {
			return {
				// 0未发货 1已发货 2已收货
				billStypes: [
					{name: '签到', status: 1}, 
					{name: '消费返', status: 2}, 
					{name: '兑换商品', status: 3},
					{name: '登录返', status: 4},
				],
				loadStatus: 'loadmore',
				filter: {
					currentStype: 0,
					page: 1
				},
				navbarCurrent: 0,
				records: []
			}
		},
		methods:{
			billStypesChange(index) {
				this.navbarCurrent = index
				this.filter.currentStype = this.billStypes[this.navbarCurrent].status
			},
			getRecords() {
				if ( this.loadStatus == 'nomore' ) {
					return console.log('nomore')
				}
				
				uni.showLoading({
					title: '加载中'
				})
				this.loadStatus = 'loading'
				getUserGoldsBills(this.filter.currentStype,this.filter.page).then(res => {
					uni.hideLoading()
					let _result = res.data || []
					if ( !_result.length ) {
						return this.loadStatus = 'nomore'
					}
					this.records = uni.$lv.array.union(this.records , _result)
					this.loadStatus = 'loadmore'
				})
			}
		},
		mounted() {
			this.getRecords()
		},
		/**
		 * 下拉刷新回调函数
		 */
		onPullDownRefresh() {
			this.loadStatus = 'loadmore'
			this.filter.page = 1
			this.records = []
			this.getRecords()
			uni.stopPullDownRefresh()
		},
		/**
		 * 上拉加载回调函数
		 */
		onReachBottom() {
			this.filter.page++
			this.getRecords()
		},
		watch: {
			'filter.currentStype': function(newVal , oldVal) {
				this.loadStatus = 'loadmore'
				this.records = []
				this.getRecords()
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: $uni-bg-color-grey;
	}
	.order-index {
		padding-bottom: 30rpx;
		.order-list {
			padding: 30rpx;
			.order-item {
				padding: 30rpx;
				background-color: #FFFFFF;
				border-radius: 20rpx;
				&__info {
					.title{
						font-size: 30rpx;
						margin-right: 20rpx;
						color: $uni-text-color-placeholder;
						width: 130rpx;
						@include flex;
						align-items: center;
					}
					.value {
						font-size: 32rpx;
						color: #333;
						flex: 1;
					}
				}
				&__info:not(:last-child){margin-bottom: 20rpx;}
			}
			.order-item:not(:last-child){margin-bottom: 40rpx;}
		}
	}
</style>
